﻿using AvaVSExt.Attributes;
using AvaVSExt.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvaVSExt
{
    public partial class Program
    {
        [VSCommand("vmmvvm")]
        public static void RunSwitchVM2MVVM()
        {
            var info = VSHelper.GetCurrentEditItemInfo();
            if (info is null) return;

            var type = SwitchVM2MVVM.GetFileType(info.ItemFullPath);
            var convertedShortPath = SwitchVM2MVVM.GetConvertFilePath(type, info.ItemShortPath, info.ProjectDirPath);
            var convertedFullPath = Path.Combine(info.ProjectDirPath, convertedShortPath);

            if (File.Exists(convertedFullPath))
            {
                VSHelper.Open(convertedFullPath);
            }
        }

    }

    public static class SwitchVM2MVVM
    {
        static string[] _viewEndNameArray = new string[] { "View.xaml", "View.axaml", "UserControl.xaml", "UserControl.axaml", "Page.xaml", "Page.axaml" };
        static string[] _viewCsEndNameArray = new string[] { "View.xaml.cs", "View.axaml.cs", "UserControl.xaml.cs", "UserControl.axaml.cs", "Page.xaml.cs", "Page.axaml.cs" };
        static string[] _viewNameArray = new string[] { "UserControl", "Page", "View" };
        static string[] _viewEndExtensionArray = new string[] { ".xaml", ".axaml" };

        public enum FileType
        {
            Unknown, View, ViewModel, ViewCS,
        }


        public static FileType GetFileType(string relPath)
        {
            if (_viewEndNameArray.Any(i => relPath.EndsWith(i, StringComparison.OrdinalIgnoreCase))) return FileType.View;
            if (_viewCsEndNameArray.Any(i => relPath.EndsWith(i, StringComparison.OrdinalIgnoreCase))) return FileType.ViewCS;
            if (relPath.EndsWith("ViewModel.cs", StringComparison.OrdinalIgnoreCase)) return FileType.ViewModel;

            return FileType.Unknown;
        }

        public static string GetConvertFilePath(FileType fileType, string relPath, string projectDir)
        {
            var result = string.Empty;
            switch (fileType)
            {
                case FileType.View:
                    result = relPath;
                    foreach (var item in _viewNameArray)
                    {
                        result = result.Replace(item, "ViewModel");
                    }
                    result = result.Replace(".xaml", ".cs").Replace(".axaml", ".cs");
                    break;
                case FileType.ViewModel:
                    result = relPath;

                    foreach (var viewName in _viewNameArray)
                    {

                        result = string.Empty;
                        foreach (var ext in _viewEndExtensionArray)
                        {
                            result = relPath.Replace("ViewModel", viewName).Replace(".cs", ext);
                            var fullPath = Path.Combine( projectDir , result);
                            if (File.Exists(fullPath)) break;

                            result = string.Empty;
                        }

                        if (string.IsNullOrWhiteSpace(result) == false) break;
                    }
                    break;
                case FileType.ViewCS:
                    result = relPath;
                    foreach (var item in _viewNameArray)
                    {
                        result = result.Replace(item, "ViewModel");
                    }
                    result = result.Replace(".xaml.cs", ".cs").Replace(".axaml.cs", ".cs");
                    break;
                default:
                    break;
            }

            return result;
        }

    }
}
